/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.realriu.snipzel.model;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author riu
 */
public abstract class Comparators {

    public static class AlphabeticTitleComparator implements Comparator<CodeSnippet>{

        @Override
        public int compare(CodeSnippet o1, CodeSnippet o2) {
            return o1.getTitle().toLowerCase().compareTo(o2.getTitle().toLowerCase());
        }

    }
    
    
    public static class TitleSearchComparator implements Comparator<CodeSnippet>{

        private String name;

        public TitleSearchComparator(String name) {
            this.name = name.toLowerCase();
        }
        
        
        @Override
        public int compare(CodeSnippet o1, CodeSnippet o2) {
            String name1 = o1.getTitle().toLowerCase();
            String name2 = o2.getTitle().toLowerCase();


            if(name1.contains(name) && !name2.contains(name)){
                return -1;
            }else if(name2.contains(name) && !name1.contains(name)){
                return 1;
            }else if(name1.contains(name) && name2.contains(name)){
                if(name1.startsWith(name) && !name2.startsWith(name)){
                    return -1;
                }
                if(!name1.startsWith(name) && name2.startsWith(name)){
                    return 1;
                }
                if(name1.startsWith(name) && name2.startsWith(name)){
                    return name1.compareTo(name2);
                }
            }

            
            return name1.compareTo(name2);
            
        }

    }
    
    
    public static class TitleAndTagSearchComparator implements Comparator<CodeSnippet>{

        private String search;

        public TitleAndTagSearchComparator(String search) {
            this.search = search.toLowerCase();
        }


        @Override
        public int compare(CodeSnippet o1, CodeSnippet o2) {
            String name1 = o1.getTitle().toLowerCase() + o1.getLanguage() + " " + o1.getTags();
            String name2 = o2.getTitle().toLowerCase() + o2.getLanguage() + " " + o2.getTags();


            if(name1.contains(search) && !name2.contains(search)){
                return -1;
            }else if(name2.contains(search) && !name1.contains(search)){
                return 1;
            }else if(name1.contains(search) && name2.contains(search)){
                if(name1.startsWith(search) && !name2.startsWith(search)){
                    return -1;
                }
                if(!name1.startsWith(search) && name2.startsWith(search)){
                    return 1;
                }
                if(name1.startsWith(search) && name2.startsWith(search)){
                    return name1.compareTo(name2);
                }
            }


            return name1.compareTo(name2);

        }

    }


}
